/**
 * Copyright (C) 2009 Matteo Merli <mm@merlimat.org>
 * 
 * $Id: PxlFile.java 17 2009-01-12 12:36:59Z matteo.merli $
 * $URL: http://pxl-lang.googlecode.com/svn/trunk/src/main/java/pxl/types/PxlFile.java $
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pxl.types;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import pxl.errors.IoError;
import pxl.types.annotations.Exported;

public class PxlFile extends PxlObject
{

    private String fileName;
    
    private BufferedReader reader;
    private BufferedWriter writer;

    public PxlFile( String fileName )
    {
        this.fileName = fileName;

        try {
            reader = new BufferedReader( new FileReader( fileName ) );
        } catch ( IOException e ) {
            throw new IoError( "Cannot open file: " + e.getMessage() );
        }
    }
    
    public PxlFile( String fileName, boolean read, boolean write ) 
    {
        this.fileName = fileName;

        try {
            reader = new BufferedReader( new FileReader( fileName ) );
            // writer = new BufferedWriter( new FileWriter( fileName ) );
        } catch ( IOException e ) {
            throw new IoError( "Cannot open file: " + e.getMessage() );
        }
    }
    
    @Override
    public String getType()
    {
        return "File";
    }

    @Override
    @Exported
    public PxlString __repr__() 
    {
        StringBuilder sb = new StringBuilder();
        sb.append( "<File '" );
        sb.append( fileName );
        sb.append( "'>" );
        return new PxlString( sb.toString() );
    }

    // Attributes

    private static final Map<String,PxlObject> attrs = PxlObject.getExportedMethods( PxlFile.class );

    @Override
    protected PxlObject getAttribute( String name )
    {
        return attrs.get( name );
    }
    
    @Override
    protected Collection<String> getAttributeNames() 
    {
        return attrs.keySet();
    }

    @Exported
    @Override
    public PxlIterator __getiterator__() {
        return new FileIterator( reader );
    }

    @Exported
    public PxlIterator lines() {
        return __getiterator__();
    }

    private static class FileIterator extends PxlIterator
    {
        private BufferedReader reader;

        public FileIterator( BufferedReader reader )
        {
            this.reader = reader;
        }

        @Override
        public PxlBoolean hasNext() 
        {
            try {
                return PxlBoolean.valueOf( reader.ready() );
            } catch ( IOException e ) {
                throw new IoError( "Read error: " + e.getMessage() );
            }        
        }

        @Override
        public PxlObject next() 
        {
            try {
                return new PxlString( reader.readLine() );
            } catch ( IOException e ) {
                throw new IoError( "Read error: " + e.getMessage() );
            }        
        }

        @Override
        public void remove() 
        {
            // ignore
        }
        
    }

}
